<template>
  <div class="manage">
    <el-dialog
      :title="operateType === 'add' ? '新增用户' : '更新用户'"
      :visible.sync="isShow"
    >
      <common-form
        :formLabel="operateFormLabel"
        :form="operateForm"
        :inline="true"
        @event-validate="validateForm"
        ref="form"
      ></common-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </el-dialog>
    <div class="manage-header">
        <el-col :span="6">
          <el-button type="primary" @click="addUser" class="addbtn">+ 新增</el-button>  
        </el-col>
        <el-col :span="6">
          <el-col :span="18">
            <el-input v-model="search" placeholder="请输入查询的内容" ></el-input>
          </el-col>
         <el-col :span="6">
           <el-button type="primary" @click="addUser" class="addbtn">查询</el-button>
         </el-col>
        </el-col>
    </div>

    <common-table
      :tableData="tableData"
      :tableLabel="tableLabel"
      :config="config"
      @changePage="getList"
      @edit="editUser"
      @del="delUser"
    >
    </common-table>
  </div>
</template>
<script>
import CommonForm from "../../src/components/CommonForm.vue";
import CommonTable from "../../src/components/CommonTable.vue";
import { getUser } from "../../api/data";
export default {
  components: {
    CommonForm,
    CommonTable,
  },
  created() {
    this.getList()
  },
  data() {
    return {
      falg: false, // 子表单校验结果
      operateType: 0, //0表示新增 1表示编辑
      isShow: false,
      search:null,
      operateFormLabel: [
        {
          model: "name",
          label: "姓名",
          type: "input",
        },
        {
          model: "age",
          label: "年龄",
          type: "input",
        },
        {
          model: "sex",
          label: "性别",
          type: "select",
          opts: [
            {
              label: "男",
              value: 1,
            },
            {
              label: "女",
              value: 0,
            },
          ],
        },
        {
          model: "birth",
          label: "出生日期",
          type: "date",
        },
        {
          model: "addr",
          label: "地址",
          type: "input",
        },
      ],
      operateForm: {
        name: "",
        addr: "",
        age: "",
        birth: "",
        sex: "",
      },
      formLabel: [
        {
          model: "keyword",
          label: "",
          type: "input",
        },
      ],
      searchFrom: {
        keyword: "",
      },
      tableData: [],
      tableLabel: [
        {
          porp: "name",
          label: "姓名",
        },
        {
          porp: "age",
          label: "年龄",
        },
        {
          porp: "sex",
          label: "性别",
        },
        {
          porp: "birth",
          label: "出生日期",
          width: 300,
        },
        {
          porp: "addr",
          label: "地址",
          width: 420,
        },
      ],
      config: {
        page: 1,
        total: 200,
      },
    };
  },
  methods: {
    confirm() { // 确认
      if (this.operateType === 1) { // 为编辑模式
        this.$http.post("/user/edit", this.operateForm).then((res) => {
          console.log(res);
          this.isShow = false;
          this.getList();
        });
      } else { // 为添加模式
      // console.log(this.$refs.form.onvalidatorFrom());
      this.$refs.form.onvalidatorFrom()
        if(!this.falg) return
        // this.$http.post("/user/add", this.operateForm).then((res) => {
        //   console.log(res);
        //   this.isShow = false;
        //   this.getList();
        // });
      }
    },
    addUser() {
      this.isShow = true;
      this.operateType = 0;
      this.operateForm = {
        name: "",
        addr: "",
        age: "",
        birth: "",
        sex: "",
      };
      // console.log(this.operateForm);
    },
    getList(page) {
      this.config.page = page
      getUser(this.config).then(({ data:res})=>{
      this.tableData = this.tableData = res.list.map((item) => {
         item.sex =  item.sex === 0 ? "女" : "男"
          return item;
        });
    })
    },
    //编辑用户
    editUser(row) {
      this.operateType = 1;
      this.isShow = true;
      this.operateForm = JSON.parse(JSON.stringify(row));
      console.log(this.operateForm);
    },
    //删除用户
    delUser(row) {
      this.$confirm("该操作将永久删除该文件，是否继续", "提示", {
        confirmBUttonText: "确认",
        cancelBUttonText: "取消",
        type: "warning",
      }).then(() => {
        const id = row.id;
        this.$http
          .get("user/del", {
            params: { id },
          })
          .then(() => {
            this.$message({
              type: "success",
              message: "删除成功",
            });
            this.getList();
          });
      });
    },
    // 获取子表单的校验数据
    validateForm(v,res){
        this.falg = v
        console.log(res);
    } 
  },
  mounted(){
    // this.getList()
  },
};
</script>
<style lang="less" scoped>
.manage-header {
  display: flex;
  justify-content: space-between;
  align-content: center;
  padding-bottom: 10px;
}
.addbtn{
  height: 40px;
}
</style>